package cn.wizzer.app.web.commons.ext.websocket.action.group;

import cn.wizzer.app.user.modules.models.constant.ChatConstant;
import cn.wizzer.app.user.modules.services.GroupLogService;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.param.Param4MapVerify;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * 群聊历史消息
 *
 * @author .
 */
@IocBean
@NettyAction(uri = "/group/history", filters = {TokenFilter.class})
public class HistoryAction extends BaseNettyAction {

    private static final Log log = Logs.get();

    @Inject
    @Reference
    private GroupLogService groupLogService;

    @Inject
    private RedisService redisService;

    @Override
    public Result action(Channel income, @Param4MapVerify(fields = {@ParamVerify(name = "groupId", required = true)}) JSONObject params) {
        String groupId = params.getString("groupId");
        String userId = params.getString("appid");
        log.info("查询群历史消息," + "groupId:" + groupId + ",userId:" + userId +
                "pageNumber:" + params.getInteger("pageNumber") + "pageNumber:" + params.getInteger("pageSize"));
        long l = System.currentTimeMillis();
        Pagination pagination = groupLogService.listPage(params.getInteger("pageNumber"), params.getInteger("pageSize"), groupId);
        log.info("查询群历史消息结束,groupId:" + groupId + ",userId:" + userId + ",耗时:" + (System.currentTimeMillis() - l) + "ms");
        //修改未读记录为已读，未读数量变更为0
        log.info("开始设置该用户群消息状态为已读,groupId:" + groupId + ",userId:" + userId);
        long l2 = System.currentTimeMillis();
        String redisKey = ChatConstant.UG + ":" + userId + ":" + groupId;
        redisService.set(redisKey, "true");
        log.info("设置该用户群消息状态为已读,groupId:" + groupId + ",userId:" + userId+ ",耗时:" + (System.currentTimeMillis() - l2) + "ms");
        return Result.success().addData(pagination);
    }

}
